﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MedicineTracker.Model;
using System.ComponentModel;
using System.Collections.ObjectModel;

namespace MedicineTracker.ViewModel
{
    public class SuppliersViewModel: Database, INotifyPropertyChanged
    {
        private ObservableCollection<Supplier> _suppliers;

        public ObservableCollection<Supplier> Suppliers
        {
            get { return _suppliers; }
        }

        public SuppliersViewModel()
        {
            _suppliers = new ObservableCollection<Supplier>();
            using (Connection)
            {
                try
                {
                    Connection.Open();
                    Command.CommandText = "SELECT * FROM suppliers";
                    Command.Connection = Connection;
                    DataReader = Command.ExecuteReader();
                    while (DataReader.Read())
                    {
                        _suppliers.Add(new Supplier
                        {
                            ID = Convert.ToInt32(DataReader["id"]),
                            Name = (string)DataReader["name"],
                            Address = (string)DataReader["address"]
                        });
                    }
                }
                catch
                {
                }
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        private void RaisePropertyChanged(string propertyName)
        {
            PropertyChangedEventHandler handler = PropertyChanged;
            if (handler != null)
            {
                handler(this, new PropertyChangedEventArgs(propertyName));
            }
        }
    }
}
